package com.samoilovich.belarustoday.basefragments;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.mainscreen.fragments.InfoFragment;

public class BaseFragmentActivity extends FragmentActivity {
    protected boolean mUpdateButtonState;
    private boolean mPictureLoadingState;

    public void setOptionsFragment(String backTitle) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        OptionsMenuBaseFragment fragment = new OptionsMenuBaseFragment();
        fragment.setUpdateButtonState(mUpdateButtonState);
        fragment.setBackTitle(backTitle);
        transaction.replace(R.id.frameLayout_content5, fragment, Constants.SLIDE_MENU_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setInfoFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content6, new InfoFragment(), Constants.INFO_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setPictureLoadingState(boolean state) {
        mPictureLoadingState = state;
    }

    public boolean getPictureLoadingState() {
        return mPictureLoadingState;
    }

    public void updateAllData() {

    }

    public void update() {
        BaseFragment fragment = null;
        FragmentManager manager = getSupportFragmentManager();

        if (manager.findFragmentById(R.id.frameLayout_content4) != null) {
            fragment = (BaseFragment) manager.findFragmentById(R.id.frameLayout_content4);
        } else {
            if (manager.findFragmentById(R.id.frameLayout_content3) != null) {
                fragment = (BaseFragment) manager.findFragmentById(R.id.frameLayout_content3);
            } else {
                if (manager.findFragmentById(R.id.frameLayout_content2) != null) {
                    fragment = (BaseFragment) manager.findFragmentById(R.id.frameLayout_content2);
                } else {
                    if (manager.findFragmentById(R.id.frameLayout_content) != null) {
                        fragment = (BaseFragment) manager.findFragmentById(R.id.frameLayout_content);
                    }
                }
            }
        }

        if (fragment != null) {
            fragment.updateAllData(getApplicationContext());
        } else {
            updateAllData();
        }
    }
}